package de.szut.ita13.sommerfeld.loganalyser;

public class LogLineParser {


	public static String getIPAdress(String logline) {
		return logline.split(" ")[0];
	}
	
	public static String getHyphen(String logline) {
		return logline.split(" ")[1];
	}
	
	public static String getUserID(String logline) {
		return logline.split(" ")[2];
	}
	
	public static String getDate(String logline) {
		return logline.substring(logline.indexOf("[") + 1, logline.indexOf("]") - 6).substring(0, logline.substring(logline.indexOf("[") + 1, logline.indexOf("]") - 6).indexOf(":"));
	}
	
	public static String getTime(String logline) {
		return logline.substring(logline.indexOf("[") + 1, logline.indexOf("]") - 6).substring(logline.substring(logline.indexOf("[") + 1, logline.indexOf("]") - 6).indexOf(":") + 1, logline.substring(logline.indexOf("[") + 1, logline.indexOf("]") - 6).length());
	}
	
	public static String getTimeZone(String logline) {
		return logline.substring(logline.indexOf("[") + 1, logline.indexOf("]")).substring(logline.substring(logline.indexOf("[") + 1, logline.indexOf("]")).length() - 5, logline.substring(logline.indexOf("[") + 1, logline.indexOf("]")).length());
	}
	
	public static String getRequestCommand(String logline) {
		return logline.split(" ")[5].substring(1,logline.split(" ")[5].length());
	}
	
	public static String getResource(String logline) {
		return logline.split(" ")[5].substring(1, logline.split(" ")[5].length()) + logline.split(" ")[6];
	}
	
	public static String getUsedProtocol(String logline) {
		return logline.split(" ")[7].substring(0, logline.split(" ")[7].length() - 1);
	}
	
	public static String getStatus(String logline) {
		return logline.split(" ")[8];
	}
	
	public static String getSize(String logline) {
		return (logline.split(" ")[9].equals("-")) ? logline.split(" ")[9].replace("-", "0") : logline.split(" ")[9];
	}
	
	public static Log parseLineToLog(String logline) {
		int loglength = logline.split(" ").length;
		if(loglength == LogHandler.LOG_LENGTH) {
			String ip = getIPAdress(logline);
			String hyphen = getHyphen(logline);
			String userid = getUserID(logline);
			String date = getDate(logline);
			String time = getTime(logline);
			String requestcmd = getRequestCommand(logline);
			String resource = getResource(logline);
			String protocol = getUsedProtocol(logline);
			String status = getStatus(logline);
			String size = getSize(logline);
			Log log = new Log(ip,hyphen,userid,new Date(Integer.parseInt(date.split("/")[0]),date.split("/")[1],Integer.parseInt(date.split("/")[2]),Integer.parseInt(time.split(":")[0]),Integer.parseInt(time.split(":")[1]),Integer.parseInt(time.split(":")[2])),requestcmd,resource,protocol,Integer.parseInt(status),Integer.parseInt(size));
			return log;
		} else {
			return null;
		}
	}
	
	public static boolean lineAvailable(String logline) {
		return logline.split(" ").length == LogHandler.LOG_LENGTH;
	}
	
	public static Date getDateFromString(String dateString, String timeString) {
		return new Date(Integer.parseInt(dateString.split("/")[0]),dateString.split("/")[1],Integer.parseInt(dateString.split("/")[2]),Integer.parseInt(timeString.split(":")[0]),Integer.parseInt(timeString.split(":")[1]),Integer.parseInt(timeString.split(":")[2]));
	}
	
}
